//
//  HotDetailViewController.m
//  Journey
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HotDetailViewController.h"
@interface HotDetailViewController ()

@end

@implementation HotDetailViewController



//-(void)viewWillAppear:(BOOL)animated{
//    
//
//    
//    
//    
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  //  self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden = NO;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];
//
   

 NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle] selectHot:userName withCompare:NO];
//    NSLog(@"%@",array);
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"loginStatus"]isEqualToString:@"1"]) {
//        
    //    NSLog(@"1111");
        
        if ([array containsObject:self.hotModel.title]) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"red"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)] ;
        //    NSLog(@"222222");
        }else{
          
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"collect"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)] ;
            
         //   NSLog(@"33333");

         }
     

//    } else{
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"collect"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)] ;
//     //   NSLog(@"4444");
//    }
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.HotString]]]];
    //是否自动缩放以适应屏幕
    [webView setScalesPageToFit:YES];
    
    
 //   NSLog(@"%@",self.HotString);
    [self.view addSubview:webView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
 }

-(void)leftButtonAction{
        
   [self.navigationController popViewControllerAnimated:YES];
   
}


-(void)rightButtonAction:(UIBarButtonItem *)button{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"userName"];
    
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]selectHot:userName withCompare:NO];
  
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    [hud hide:YES afterDelay:0.5];
    
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"loginStatus"]isEqualToString:@"0"]) {
//        
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"需要微博登录后收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
//        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            
//            
//        }]];
//        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//           //登录第二步
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
//        
//        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//            
//            //          获取微博用户名、uid、token等
//            if (response.responseCode == UMSResponseCodeSuccess) {//登录成功
//                
//                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
//                //       NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//                //
//                //                [defaults setObject:snsAccount.userName forKey:@"userName"];
//                //                [defaults setObject:snsAccount.iconURL forKey:@"headUrl"];
//                //                [defaults synchronize];
//                
//                NSLog(@"%@, %@",snsAccount.userName, snsAccount.iconURL);
//                
//                
//                //登录第三步 ：在登录成功后 获取用户信息
//                //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
//                [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
//                    
//                    [defaults setObject:snsAccount.userName forKey:@"userName"];
//                    [defaults setObject:snsAccount.iconURL forKey:@"headUrl"];
//                    [defaults synchronize];
//                    
//                    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"loginStatus"];
//                    
//                //    [self.loginButton setTitle: snsAccount.userName forState:UIControlStateNormal];
//               //     [self.imageButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:snsAccount.iconURL]]] forState:UIControlStateNormal];
//                    //    NSLog(@"SnsInformation is %@",response.data);
//                }];
//                
//            }
//            
//        });
//   }]];
//        
//       [self presentViewController:alert animated:YES completion:nil];
//
//    }else {
        if ([array containsObject:self.hotModel.title] ) {
            
            [[DataBaseHandle shareDataBaseHandle]hotDeleteTable:self.hotModel.title];
            [array removeLastObject];
            hud.labelText = @"取消收藏";
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"collect"];
        //    NSLog(@"111");
            
        } else {
            [[DataBaseHandle shareDataBaseHandle]insertHot:self.hotModel withUserName:userName];
            hud.labelText = @"收藏成功";
        //    NSLog(@"%@",self.hotModel.title);
            self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"red"];
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isCollect"];
        }
    }
 
//}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
